The first step to getting up and running with Grails is to install the distribution. To do so follow these steps:
- Download a binary distribution of Grails and extract the resulting zip file to a location of your choice
- Set the GRAILS_HOME environment variable to the location where you extracted the zip
- On Unix/Linux based systems this is typically a matter of adding something like the following
export GRAILS_HOME=/path/to/grails
to your profile
- On Windows this is typically a matter of setting an environment variable under
My Computer/Advanced/Environment Variables
- Now you need to add the
bin
directory to your PATH
variable:
- On Unix/Linux base system this can be done by doing a
export PATH="$PATH:$GRAILS_HOME/bin"
- On windows this is done by modifying the
Path
environment variable under My Computer/Advanced/Environment Variables
If Grails is working correctly you should now be able to type grails
in the terminal window and see output similar to the below:
Welcome to Grails 1.0 - http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: /Developer/grails-1.0
No script name specified. Use 'grails help' for more info
To create a Grails application you first need to familiarize yourself with the usage of the grails
command which is used in the following manner:In this case the command you need to execute is create-app:
grails create-app helloworld
This will create a new directory inside the current one that contains the project. You should now navigate to this directory in terminal:
cd helloworld
To implement the typical "hello world!" example run the create-controller command:
grails create-controller hello
This will create a new controller (Refer to the section on Controllers for more information) in the grails-app/controllers
directory called HelloController.groovy
.Controllers are capable of dealing with web requests and to fulfil the "hello world!" use case our implementation needs to look like the following:class HelloController {
def world = {
render "Hello World!"
}
}
Job done. Now start-up the container with another new command called run-app:
grails run-app
This will start-up a server on port 8080 and you should now be able to access your application with the URL: http://localhost:8080/helloworld
The result will look something like the following screenshot:This is the Grails intro page which is rendered by the web-app/index.gsp
file. You will note it has a detected the presence of your controller and clicking on the link to our controller we can see the text "Hello World!" printed to the browser window.IntelliJ IDEA
Currently by far the most mature and comprehensive Groovy & Grails IDE is IntelliJ IDEA 7.0 and the JetGroovy plug-in. The Grails team recommends IDEA over other IDE environments for large projects.TextMate
Since Grails' focus is on simplicity it is often possible to utilize more simple editors and TextMate on the Mac has an excellent Groovy/Grails bundle available from the Texmate bundles SVN.Eclipse
For Eclipse there is also the Groovy Eclipse Plugin that offers syntax highlighting, code completion and so on.
There are some quirks with the Groovy Eclipse plug-in which are covered in detail on the Grails wiki.
Grails automatically creates Eclipse .project
and classpath
files for you, so to import a Grails project just right-click in the "Package Explorer" and select "Import" then "Existing project into Workspace" and "Browse" to the location of your project.Then immediately click "Ok" followed by "Finish" and your project will be set-up.Grails will also automatically set-up an appropriate Eclipse "Run Configuration", that can be accessed from the "Run" menu in Eclipse.Grails uses "convention over configuration" to configure itself. This typically means that the name and location of files is used instead of explicit configuration, hence you need to familiarize yourself with the directory structure provided by Grails.Here is a breakdown and links to the relevant sections:
grails-app
- top level directory for Groovy sources
scripts
- Gant scripts.
src
- Supporting sources
groovy
- Other Groovy sources
java
- Other Java sources
test
- Unit and integration tests.
Grails applications can be run with the built in Jetty server using the run-app command which will load a server on port 8080 by default:You can specify a different port by using the server.port
argument:grails -Dserver.port=8090 run-app
More information on the run-app command can be found in the reference guide.The create-*
commands in Grails automatically create integration tests for you within the test/integration
directory. It is of course up to you to populate these tests with valid test logic, information on which can be found in the section on Testing. However, if you wish to execute tests you can run the test-app command as follows:Grails also automatically generates an Ant build.xml
which can also run the tests by delegating to Grails' test-app command:This is useful when you need to build Grails applications as part of a continuous integration platform such as CruiseControl.Grails applications are deployed as Web Application Archives (WAR files), and Grails includes the war command for performing this task:This will produce a WAR file in the root of your project which can then be deployed as per your containers instructions.
NEVER deploy Grails using the run-app command as this command sets Grails up for auto-reloading at runtime which has a severe performance and scalability implication
When deploying Grails you should always run your containers JVM with the -server
option and with sufficient memory allocation. A good set of VM flags would be:Grails supports a pretty wide range of containers including:
- Tomcat 5.5
- Tomcat 6.0
- GlassFish v1 (Sun AS 9.0)
- GlassFish v2 (Sun AS 9.1)
- Sun App Server 8.2
- Websphere 6.1
- Websphere 5.1
- Resin 3.2
- Oracle AS
- JBoss 4.2
- Jetty 6.1
- Jetty 5
- Weblogic 7/8/9/10
Some containers have bugs however, which in most cases can be worked around. A list of known deployment issues can be found on the Grails wiki.Grails ships with a few convenience targets such as create-controller, create-domain-class and so on that will create Controllers and different artefact types for you.
These are merely for your convenience and you can just as easily use an IDE or your favourite text editor.
For example to create the basis of an application you typically need a domain model:grails create-domain-class book
This will result in the creation of a domain class at grails-app/domain/Book.groovy
such as:There are many such create-*
commands that can be explored in the command line reference guide.To get started quickly with Grails it is often useful to use a feature called Scaffolding to generate the skeleton of an application. To do this use one of the generate-*
commands such as generate-all, which will generate a controller and the relevant views: